<template>
  <div>
    <!-- show-file-list:是否显示已上传文件列表 -->
    <!-- on-success:文件上传成功时的钩子 -->
    <!-- before-upload;上传文件之前的钩子，参数为上传的文件，若返回 false 或者返回 Promise 且被 reject，则停止上传。 -->
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :http-request="upload"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
      <el-progress v-show="showprogress" type="circle" :percentage="percentage" class="progress" />
    </el-upload>
  </div>
</template>

<script>
const COS = require('cos-js-sdk-v5')
// SECRETID 和 SECRETKEY请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
const cos = new COS({
  SecretId: 'AKIDGLAqKm182f8NM6dGdjZ9SUIr9BxUkjRt',
  SecretKey: 'C7l9G6fxzkjTxB8pHUR8amiPq2ye5GAn'
})
export default {
  data() {
    return {
      imageUrl: '', // 图片地址
      showprogress: false, // 进度条状态
      percentage: 0// 进度条进度
    }
  },
  methods: {
    // 自定义上传属性
    upload(res) {
      if (res.file) {
        // 上传显示进度条
        this.showprogress = true
        cos.putObject(
          {
            Bucket: 'lsy-hope-1306621536', // 存储桶的名称，必须
            Region: 'ap-beijing', // 存储桶所在地域，必须字段
            Key: res.file.name, // 文件名，必须
            StorageClass: 'STANDARD', // 上传模式, 标准模式
            Body: res.file, // 上传文件对象
            // 上传进度条
            onProgress: progressData => {
              // console.log(JSON.stringify(progressData))

              // 将进度赋值给percentage
              this.percentage = progressData.percent * 100
            }
          },
          (err, data) => {
            // 判断如果成功
            if (err === null && data.statusCode === 200) {
              this.imageUrl = `https:${data.Location}`
              // console.log(err || data)
              // 上传完成,隐藏进度条
              this.showprogress = false
            }
          }
        )
      }
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style>

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px !important;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.progress {
  position: absolute !important;
  display: flex;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #e4e4e4;
  border-radius: 50%;
  opacity: 0.9;
}
</style>
